package com.ljk.util.string;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * Base64加解密工具
 *
 * @author ljk
 * @since 2025/3/27 20:12
 */
public class Base64Util {
    private Base64Util() {
    }

    /**
     * Base64编码
     *
     * @param input 待编码字符串
     * @return 编码后的字符串
     */
    public static String base64EncodeToString(String input) {
        return new String(base64Encode(input), StandardCharsets.UTF_8);
    }

    public static String base64EncodeToString(byte[] input) {
        return new String(base64Encode(input), StandardCharsets.UTF_8);
    }

    public static String base64EncodeToString(byte[] input, Charset charset) {
        return new String(base64Encode(input), charset);
    }

    public static byte[] base64Encode(String input) {
        return base64Encode(input.getBytes(StandardCharsets.UTF_8));
    }

    public static byte[] base64Encode(byte[] input) {
        return Base64.getEncoder().encode(input);
    }

    /**
     * Base64解码
     *
     * @param input 待解码字符串
     * @return 解码后的字符串
     */
    public static String base64DecodeToString(String input) {
        return new String(base64Decode(input), StandardCharsets.UTF_8);
    }

    public static String base64DecodeToString(byte[] input) {
        return new String(base64Decode(input), StandardCharsets.UTF_8);
    }

    public static String base64DecodeToString(byte[] input, Charset charset) {
        return new String(base64Decode(input), charset);
    }

    public static byte[] base64Decode(String input) {
        return base64Decode(input.getBytes(StandardCharsets.UTF_8));
    }

    public static byte[] base64Decode(byte[] input) {
        return Base64.getDecoder().decode(input);
    }


}
